package field

import (
	"fmt"

	"gitee.com/yanwc/net/utils"
)

type (
	GSM    uint8 // GSM信号强度(0-31)
	GSMRaw [1]byte
)

func (gsm GSM) Raw() (out GSMRaw) {
	copy(out[:], utils.NumberToBytesWithBigEndian(gsm))
	return
}

func (gsm GSMRaw) Type() (GSM, error) {
	t := utils.BytesToNumber[GSM](gsm[:])
	if uint8(t) > t.Max() || uint8(t) < t.Min() {
		return 0, ErrorField(fmt.Sprintf("GSM信号强度(0-31),超出范围:%d", t))
	}

	return GSM(t), nil
}

func (gsm GSM) Max() uint8 {
	return 31
}

func (gsm GSM) Min() uint8 {
	return 1
}

func (gsm GSMRaw) Slice() []byte {
	return gsm[:]
}
